#ifndef lint
static const char yysccsid[] = "@(#)yaccpar	1.9 (Berkeley) 02/21/93";
#endif

#define YYBYACC 1
#define YYMAJOR 1
#define YYMINOR 9
#define YYPATCH 20110908

#define YYEMPTY        (-1)
#define yyclearin      (yychar = YYEMPTY)
#define yyerrok        (yyerrflag = 0)
#define YYRECOVERING() (yyerrflag != 0)

#define YYPREFIX "yy"

#define YYPURE 0

#line 86 "pascal_lr1_syntax_analyser.y"
#define YYSTYPE char *

#include <stdio.h>
#include <string.h>

typedef struct{
	int tipo;
	char *conteudo;
} node;

/*#define YYSTYPE node**/

/* struct da tabela de simbolos*/
union val
{
	int t_int;
	char t_char;
	char* t_string;
	float t_real;
	/*bool t_bool;*/
	
};
struct simbolo
{
	char* nome;
/*
	1- inteiro 2-carac 3-string 4-real 5-bool 6-constante 10-nomedefunção 11-nomedeprocedimento 12-nomedoprograma
*/
	int tipo;
	int escopo; /* 0-global  1...-função*/
	union val valor; /* ainda nao sei se há necessidade*/


};

char * t_tipo;

int ct;

/* tabela de simbolos*/
int aponta_tab = 0;
int rt_tabela = 0;
int t_escopo = 0;
struct simbolo tabela_de_simbolos[1000];

int tipo_herdado, tipo_aux;

FILE* file; 
int label = 1;
	
void create_file(char* nome)
{
	char* outro_nome = strdup(nome);
	strcat(outro_nome, ".c");
	file = fopen(outro_nome, "w");
}

void declaracao_var(char* tipo, char* identificadores)
{
	fprintf(file, "%s %s;\n", tipo, identificadores);
}

char* declara_parametros(char* identificadores, char* tipo)
{
	char* aux = strdup(identificadores);
	char* aux2 = strdup("");
	char* aux3;

	char* teste;
	while(sscanf(aux, "%s", teste) > 0)
	{
		sprintf(aux3, "%s %s ", tipo, teste);
		strcat(aux2, strdup(aux3));
		aux += strlen(teste)+1;
	}
	return aux2;
}

void declara_const()
{
	/* Faz nada ainda*/
}

void declara_tipo() 
{
	/* Faz nada ainda*/
}

void create_func(char* id, char* tipo, char* para, char* declara, char* bloco)
{
	fprintf(file, "%s %s(%s) { %s %s }\n", tipo, id, para, declara, bloco);
}

void create_proc(char* id, char* para)
{
	fprintf(file, "void %s(%s) { }\n", id, para);
}

void create_if(char* expressao, char* statements)
{
	printf("if (!%s) goto label%i", expressao, label);
}

void create_main(char* bloco) 
{
	fprintf(file, "int main(int argc, char** argv) { %s }\n", bloco);
}

#line 128 "y.tab.c"

#ifndef YYSTYPE
typedef int YYSTYPE;
#endif

/* compatibility with bison */
#ifdef YYPARSE_PARAM
/* compatibility with FreeBSD */
# ifdef YYPARSE_PARAM_TYPE
#  define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)
# else
#  define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)
# endif
#else
# define YYPARSE_DECL() yyparse(void)
#endif

/* Parameters sent to lex. */
#ifdef YYLEX_PARAM
# define YYLEX_DECL() yylex(void *YYLEX_PARAM)
# define YYLEX yylex(YYLEX_PARAM)
#else
# define YYLEX_DECL() yylex(void)
# define YYLEX yylex()
#endif

/* Parameters sent to yyerror. */
#define YYERROR_DECL() yyerror(const char *s)
#define YYERROR_CALL(msg) yyerror(msg)

extern int YYPARSE_DECL();

#define PROGRAM 257
#define PROCEDURE 258
#define FUNCTION 259
#define CONST 260
#define TYPE 261
#define VAR 262
#define ARRAY 263
#define BOOLEAN 264
#define CHAR 265
#define INTEGER 266
#define REAL 267
#define RECORD 268
#define BEGIN_ 269
#define END 270
#define OF 271
#define IF 272
#define THEN 273
#define ELSE 274
#define FOR 275
#define TO 276
#define DOWNTO 277
#define DO 278
#define NEW 279
#define DISPOSE 280
#define VIRGULA 281
#define PONTO_E_VIRGULA 282
#define DOIS_PONTOS 283
#define PONTO 284
#define OPERADOR_DE_ATRIBUICAO 285
#define OPERADOR_DE_SUBFAIXA 286
#define SOMA 287
#define SUBTRACAO 288
#define ASTERISCO 289
#define DIVISAO 290
#define ENDERECO 291
#define VALOR_DE_PONTEIRO 292
#define MOD 293
#define AND 294
#define OR 295
#define NOT 296
#define IGUALDADE 297
#define DESIGUALDADE 298
#define MENOR 299
#define MENOR_OU_IGUAL 300
#define MAIOR 301
#define MAIOR_OU_IGUAL 302
#define ABRE_PARENTESIS 303
#define FECHA_PARENTESIS 304
#define ABRE_COLCHETE 305
#define FECHA_COLCHETE 306
#define CONDICIONAL_TERNARIO 307
#define VALOR_STRING 308
#define VALOR_CHAR 309
#define VALOR_INT 310
#define VALOR_FLOAT 311
#define VALOR_BOOL 312
#define IDENTIFICADOR 313
#define FIM_DE_ARQUIVO 314
#define ARROBA 315
#define WHILE 316
#define BREAK 317
#define GOTO 318
#define READ 319
#define PRINT 320
#define REPEAT 321
#define UNTIL 322
#define STEP 323
#define STRING 324
#define YYERRCODE 256
static const short yylhs[] = {                           -1,
    1,    0,    2,    2,    2,    2,    3,    3,    3,    5,
    5,    6,    6,    7,    7,   12,   12,    8,    9,   14,
   14,   14,   13,   11,   11,   11,   11,   11,   11,   11,
   11,   10,   10,   10,   10,   10,   15,   16,   18,   20,
   20,   19,   21,   21,   17,    4,   23,   23,   24,   24,
   24,   24,   24,   22,   22,   22,   25,   25,   25,   27,
   27,   27,   27,   27,   27,   27,   27,   27,   27,   27,
   27,   28,   28,   28,   28,   28,   28,   26,   26,   26,
   26,   26,
};
static const short yylen[] = {                            2,
    0,    8,    2,    2,    2,    0,    2,    2,    0,    5,
    0,    5,    0,    5,    0,    3,    1,   11,    9,    5,
    3,    0,    1,    1,    1,    1,    1,    1,    1,    1,
    1,    1,    1,    1,    1,    1,    4,    3,    3,    3,
    1,    3,    1,    1,    5,    3,    3,    0,    1,    1,
    1,    1,    3,    1,    4,    3,    3,    5,    1,    1,
    1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
    1,    1,    2,    2,    2,    3,    1,    4,    6,    4,
    4,    8,
};
static const short yydefred[] = {                         0,
    0,    0,    1,    0,    0,    0,    0,   15,    0,    0,
    3,    0,    4,    0,    0,    0,    0,    0,    0,    0,
    0,   17,    0,   23,    0,    0,    0,    0,    7,    8,
   32,   33,   34,   35,   36,    0,    0,   25,   27,   24,
   26,    0,    0,    0,    0,   28,   29,   30,   31,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
   52,    0,   49,   77,   54,    0,    0,    0,    0,   51,
   59,    2,    0,    0,    0,   44,    0,    0,    0,   41,
    0,    0,   16,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,   46,
    0,   60,   61,   69,   70,   71,   68,   66,   67,   62,
   64,   63,   65,    0,    0,   10,    0,    0,   38,    0,
    0,   39,   12,   14,    0,    0,    0,    0,    0,    0,
   76,    0,    0,   56,    0,    0,   47,    0,   57,   37,
    0,   42,   40,   21,    0,    0,    0,    0,    0,   80,
    0,   55,    0,   45,    0,    0,    0,    0,    0,   58,
   20,    0,    0,   79,    0,   19,    0,    0,    0,   82,
   18,
};
static const short yydgoto[] = {                          2,
    4,    9,   17,   63,   11,   13,   14,   18,   19,   64,
   45,   85,   65,   86,   46,   47,   48,   49,   77,   81,
   78,   88,   67,   68,   69,   70,  115,   71,
};
static const short yysindex[] = {                      -233,
 -282,    0,    0, -247, -220, -269, -263,    0, -154, -228,
    0, -219,    0, -244, -225, -225, -176, -154, -154, -182,
  -48,    0, -197,    0, -204, -190, -250, -168,    0,    0,
    0,    0,    0,    0,    0, -161, -167,    0,    0,    0,
    0, -244,   57,   57, -140,    0,    0,    0,    0, -152,
  -48, -244, -244,   68,   68,   68,   68,   68,   68,   68,
    0, -250,    0,    0,    0, -229, -121, -106,  158,    0,
    0,    0, -269,  -89, -183,    0, -118, -103, -214,    0,
 -267, -263,    0,  -95, -164, -261, -259, -214,  -25,    2,
  -72,  -72,  158,  116,   27, -133, -125,   68,   68,    0,
 -250,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,   68,   68,    0,  -48,  -48,    0,   57,
   57,    0,    0,    0,  -48, -244,  -91,  -85, -250,   68,
    0, -250,   68,    0,  158,  137,    0,  -82,    0,    0,
  -78,    0,    0,    0, -114, -220,  -48,  -71,  -56,    0,
  158,    0,   68,    0,  -48, -176,  -80, -250,   68,    0,
    0,  -74, -220,    0,   52,    0, -176, -176,  -73,    0,
    0,
};
static const short yyrindex[] = {                         0,
    0,    0,    0,    0, -157, -136, -134,    0,  -59,    0,
    0,    0,    0, -115,    0,    0,    0,  -59,  -59,    0,
    0,    0,    0,    0,    0,    0,  -35,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0, -253, -253,    0,    0,    0,    0,    0,    0,    0,
    0, -108,    0,    0,    0,  100,    0,    0,  -60,    0,
    0,    0, -136,    0,    0,    0,    0,    0, -232,    0,
    0, -134,    0,    0,    0,    0,    0, -142,    0,    0,
 -110,  -83, -186,    0,    0,    0,    0,    0,    0,    0,
 -254,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0, -201,    0,
    0,  -46,    0,    0,  -45,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,  -22,    0,  -33,    0,    0,
  -30,    0,    0,    0,    0,    0,    0,  -46,    0,    0,
    0,    0,  -22,    0,    0,    0,    0,    0,    0,    0,
    0,
};
static const short yygindex[] = {                         0,
    0, -131,  152,  -17,  177,  171,    0,    0,    0,  -10,
  -40,  -12,  159,  201,    0,    0,    0,  219,  -32,    0,
  138,  -26,  -49,    0,  -51,    0,    0,  -88,
};
#define YYTABLESIZE 465
static const short yytable[] = {                         28,
   66,   23,   89,   90,   91,   92,   93,   94,   95,   36,
   84,   80,   96,  121,  156,   48,   79,   79,   27,   48,
  126,   54,  126,    1,   55,  138,  139,   48,   22,   75,
    3,  167,   76,   76,    5,   66,   56,   57,  122,    6,
    7,    8,  127,   10,  128,   58,  135,  136,   43,   12,
   22,  137,   59,   43,   97,   98,   20,   31,   32,   33,
   34,   35,   24,   21,  160,   60,   61,   48,   22,   97,
   62,   43,   48,   43,   66,   99,  140,  141,  149,  148,
   48,  151,  150,   50,  144,   51,   73,   24,  143,   73,
   99,   73,   27,   79,   79,   73,   73,   50,   52,  118,
    6,    6,   66,   15,   16,   66,  157,  165,  164,   76,
   76,    6,   53,  145,  161,   72,   50,   73,  125,   73,
   73,   11,   11,   13,   13,   31,   32,   33,   34,   35,
   72,   66,   11,   72,   13,   72,   73,   44,  162,   72,
   72,   82,    5,    5,   72,   72,   72,   72,  100,  169,
  170,   72,   72,    5,   72,   72,   72,   72,   72,   72,
   83,   72,   74,   72,   72,   74,   50,   74,  155,   29,
   30,   74,   74,   25,   26,  101,   74,   74,   74,   74,
   72,  117,  120,   74,   74,  119,  124,  134,  133,   75,
  146,  154,   75,   74,   75,   74,   74,  147,   75,   75,
  153,  163,  158,   75,   75,   75,   75,  166,  171,    9,
   75,   75,   74,   48,   37,   38,   39,   40,   41,   42,
   75,   50,   75,   75,  108,  109,  110,  111,  112,  113,
  102,  103,  104,  105,   48,   48,   53,  106,  107,   75,
  108,  109,  110,  111,  112,  113,    6,  129,   78,  116,
  114,   81,  123,   87,   43,   74,   44,  142,    0,    0,
    0,  102,  103,  104,  105,    0,  159,    0,  106,  107,
    0,  108,  109,  110,  111,  112,  113,  130,    0,    0,
    0,  114,    0,    0,    0,    0,    0,    0,  102,  103,
  104,  105,    0,    0,    0,  106,  107,    0,  108,  109,
  110,  111,  112,  113,  132,    0,    0,    0,  114,    0,
    0,    0,    0,  102,  103,  104,  105,    0,    0,    0,
  106,  107,    0,  108,  109,  110,  111,  112,  113,  168,
    0,    0,    0,  114,    0,    0,    0,    0,  102,  103,
  104,  105,    0,    0,    0,  106,  107,    0,  108,  109,
  110,  111,  112,  113,   56,   57,    0,    0,  114,    0,
    0,    0,    0,   58,   31,   32,   33,   34,   35,   24,
   59,    0,    0,    0,    0,   31,   32,   33,   34,   35,
   24,   72,    0,    0,    0,    0,   72,   72,   72,   72,
    0,    0,    0,   72,   72,    0,   72,   72,   72,   72,
   72,   72,  102,  103,  104,  105,   72,    0,    0,  106,
  107,    0,  108,  109,  110,  111,  112,  113,    0,  131,
    0,    0,  114,  102,  103,  104,  105,    0,    0,    0,
  106,  107,    0,  108,  109,  110,  111,  112,  113,    0,
    0,    0,  152,  114,  102,  103,  104,  105,    0,    0,
    0,  106,  107,    0,  108,  109,  110,  111,  112,  113,
    0,    0,    0,    0,  114,
};
static const short yycheck[] = {                         17,
   27,   14,   54,   55,   56,   57,   58,   59,   60,   20,
   51,   44,   62,  281,  146,  270,   43,   44,  269,  274,
  282,  272,  282,  257,  275,  114,  115,  282,  282,   42,
  313,  163,   43,   44,  282,   62,  287,  288,  306,  260,
  261,  262,  304,  313,  304,  296,   98,   99,  281,  313,
  304,  101,  303,  286,  284,  285,  285,  308,  309,  310,
  311,  312,  313,  283,  153,  316,  317,  322,  313,  284,
  321,  304,  274,  306,  101,  305,  117,  118,  130,  129,
  282,  133,  132,  281,  125,  283,  273,  313,  121,  276,
  305,  278,  269,  120,  121,  282,  283,  281,  303,  283,
  258,  259,  129,  258,  259,  132,  147,  159,  158,  120,
  121,  269,  303,  126,  155,  284,  281,  304,  283,  306,
  282,  258,  259,  258,  259,  308,  309,  310,  311,  312,
  273,  158,  269,  276,  269,  278,  323,  305,  156,  282,
  283,  282,  258,  259,  287,  288,  289,  290,  270,  167,
  168,  294,  295,  269,  297,  298,  299,  300,  301,  302,
  313,  304,  273,  306,  307,  276,  281,  278,  283,   18,
   19,  282,  283,   15,   16,  282,  287,  288,  289,  290,
  323,  271,  286,  294,  295,  304,  282,  313,  322,  273,
  282,  270,  276,  304,  278,  306,  307,  283,  282,  283,
  283,  282,  274,  287,  288,  289,  290,  282,  282,  269,
  294,  295,  323,  322,  263,  264,  265,  266,  267,  268,
  304,  282,  306,  307,  297,  298,  299,  300,  301,  302,
  287,  288,  289,  290,  270,  282,  282,  294,  295,  323,
  297,  298,  299,  300,  301,  302,  269,  273,  282,   73,
  307,  282,   82,   53,  303,   37,  305,  120,   -1,   -1,
   -1,  287,  288,  289,  290,   -1,  323,   -1,  294,  295,
   -1,  297,  298,  299,  300,  301,  302,  276,   -1,   -1,
   -1,  307,   -1,   -1,   -1,   -1,   -1,   -1,  287,  288,
  289,  290,   -1,   -1,   -1,  294,  295,   -1,  297,  298,
  299,  300,  301,  302,  278,   -1,   -1,   -1,  307,   -1,
   -1,   -1,   -1,  287,  288,  289,  290,   -1,   -1,   -1,
  294,  295,   -1,  297,  298,  299,  300,  301,  302,  278,
   -1,   -1,   -1,  307,   -1,   -1,   -1,   -1,  287,  288,
  289,  290,   -1,   -1,   -1,  294,  295,   -1,  297,  298,
  299,  300,  301,  302,  287,  288,   -1,   -1,  307,   -1,
   -1,   -1,   -1,  296,  308,  309,  310,  311,  312,  313,
  303,   -1,   -1,   -1,   -1,  308,  309,  310,  311,  312,
  313,  282,   -1,   -1,   -1,   -1,  287,  288,  289,  290,
   -1,   -1,   -1,  294,  295,   -1,  297,  298,  299,  300,
  301,  302,  287,  288,  289,  290,  307,   -1,   -1,  294,
  295,   -1,  297,  298,  299,  300,  301,  302,   -1,  304,
   -1,   -1,  307,  287,  288,  289,  290,   -1,   -1,   -1,
  294,  295,   -1,  297,  298,  299,  300,  301,  302,   -1,
   -1,   -1,  306,  307,  287,  288,  289,  290,   -1,   -1,
   -1,  294,  295,   -1,  297,  298,  299,  300,  301,  302,
   -1,   -1,   -1,   -1,  307,
};
#define YYFINAL 2
#ifndef YYDEBUG
#define YYDEBUG 0
#endif
#define YYMAXTOKEN 324
#if YYDEBUG
static const char *yyname[] = {

"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"PROGRAM","PROCEDURE","FUNCTION",
"CONST","TYPE","VAR","ARRAY","BOOLEAN","CHAR","INTEGER","REAL","RECORD",
"BEGIN_","END","OF","IF","THEN","ELSE","FOR","TO","DOWNTO","DO","NEW","DISPOSE",
"VIRGULA","PONTO_E_VIRGULA","DOIS_PONTOS","PONTO","OPERADOR_DE_ATRIBUICAO",
"OPERADOR_DE_SUBFAIXA","SOMA","SUBTRACAO","ASTERISCO","DIVISAO","ENDERECO",
"VALOR_DE_PONTEIRO","MOD","AND","OR","NOT","IGUALDADE","DESIGUALDADE","MENOR",
"MENOR_OU_IGUAL","MAIOR","MAIOR_OU_IGUAL","ABRE_PARENTESIS","FECHA_PARENTESIS",
"ABRE_COLCHETE","FECHA_COLCHETE","CONDICIONAL_TERNARIO","VALOR_STRING",
"VALOR_CHAR","VALOR_INT","VALOR_FLOAT","VALOR_BOOL","IDENTIFICADOR",
"FIM_DE_ARQUIVO","ARROBA","WHILE","BREAK","GOTO","READ","PRINT","REPEAT",
"UNTIL","STEP","STRING",
};
static const char *yyrule[] = {
"$accept : program",
"$$1 :",
"program : PROGRAM IDENTIFICADOR $$1 PONTO_E_VIRGULA declarations sub_programs block PONTO",
"declarations : CONST const_declaration",
"declarations : TYPE type_declaration",
"declarations : VAR var_declaration",
"declarations :",
"sub_programs : function_declaration sub_programs",
"sub_programs : procedure_declaration sub_programs",
"sub_programs :",
"const_declaration : IDENTIFICADOR OPERADOR_DE_ATRIBUICAO valor PONTO_E_VIRGULA const_declaration",
"const_declaration :",
"type_declaration : IDENTIFICADOR DOIS_PONTOS type PONTO_E_VIRGULA type_declaration",
"type_declaration :",
"var_declaration : var_declaration id_list DOIS_PONTOS type PONTO_E_VIRGULA",
"var_declaration :",
"id_list : id_list VIRGULA IDENTIFICADOR",
"id_list : IDENTIFICADOR",
"function_declaration : FUNCTION id ABRE_PARENTESIS parameters FECHA_PARENTESIS DOIS_PONTOS type PONTO_E_VIRGULA declarations block PONTO_E_VIRGULA",
"procedure_declaration : PROCEDURE id ABRE_PARENTESIS parameters FECHA_PARENTESIS PONTO_E_VIRGULA declarations block PONTO_E_VIRGULA",
"parameters : parameters PONTO_E_VIRGULA id_list DOIS_PONTOS type",
"parameters : id_list DOIS_PONTOS type",
"parameters :",
"id : IDENTIFICADOR",
"type : INTEGER",
"type : BOOLEAN",
"type : REAL",
"type : CHAR",
"type : array",
"type : enum",
"type : record",
"type : range",
"valor : VALOR_STRING",
"valor : VALOR_CHAR",
"valor : VALOR_INT",
"valor : VALOR_FLOAT",
"valor : VALOR_BOOL",
"array : ARRAY range OF type",
"enum : ABRE_PARENTESIS subrange FECHA_PARENTESIS",
"range : ABRE_COLCHETE list_subrange FECHA_COLCHETE",
"list_subrange : list_subrange VIRGULA subrange",
"list_subrange : subrange",
"subrange : regra_x OPERADOR_DE_SUBFAIXA regra_x",
"regra_x : access",
"regra_x : valor",
"record : RECORD id_list DOIS_PONTOS type END",
"block : BEGIN_ statements END",
"statements : statement PONTO_E_VIRGULA statements",
"statements :",
"statement : block",
"statement : expression",
"statement : control",
"statement : BREAK",
"statement : access OPERADOR_DE_ATRIBUICAO expression",
"access : id",
"access : access ABRE_COLCHETE expression FECHA_COLCHETE",
"access : access PONTO IDENTIFICADOR",
"expression : expression oper1 factor",
"expression : expression CONDICIONAL_TERNARIO factor DOIS_PONTOS factor",
"expression : factor",
"oper1 : SOMA",
"oper1 : SUBTRACAO",
"oper1 : MENOR",
"oper1 : MAIOR",
"oper1 : MENOR_OU_IGUAL",
"oper1 : MAIOR_OU_IGUAL",
"oper1 : IGUALDADE",
"oper1 : DESIGUALDADE",
"oper1 : OR",
"oper1 : ASTERISCO",
"oper1 : DIVISAO",
"oper1 : AND",
"factor : access",
"factor : NOT expression",
"factor : SOMA expression",
"factor : SUBTRACAO expression",
"factor : ABRE_PARENTESIS expression FECHA_PARENTESIS",
"factor : valor",
"control : IF expression THEN statements",
"control : IF expression THEN statements ELSE statements",
"control : WHILE expression DO statements",
"control : REPEAT statements UNTIL expression",
"control : FOR expression TO expression STEP expression DO block",

};
#endif

int      yydebug;
int      yynerrs;

int      yyerrflag;
int      yychar;
YYSTYPE  yyval;
YYSTYPE  yylval;

/* define the initial stack-sizes */
#ifdef YYSTACKSIZE
#undef YYMAXDEPTH
#define YYMAXDEPTH  YYSTACKSIZE
#else
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 500
#define YYMAXDEPTH  500
#endif
#endif

#define YYINITSTACKSIZE 500

typedef struct {
    unsigned stacksize;
    short    *s_base;
    short    *s_mark;
    short    *s_last;
    YYSTYPE  *l_base;
    YYSTYPE  *l_mark;
} YYSTACKDATA;
/* variables for the parser stack */
static YYSTACKDATA yystack;
#line 449 "pascal_lr1_syntax_analyser.y"

/*
** CODE SECTION
*/

#include "lex.yy.c"



struct simbolo tabela[100];
int posicao_na_tabela = 0;


int yyerror(char *message) {
	mensagemErro(message, lc);
	return 1;
}

int main(int argc, char** argv) {

	yyin = fopen(argv[1], "r");
	yyparse( );

	printf("tabela de simbolos\n");	
	for (ct=0;ct<aponta_tab;ct++)
	{
		printf("Simbolo-> %s\t Tipo->%d\t Escopo->%d\n",tabela_de_simbolos[ct].nome,tabela_de_simbolos[ct].tipo,tabela_de_simbolos[ct].escopo);	
			
	}


	if (ha_erro==0) // para erros sintaticos
	printf("\n\nPROGRAMA COMPILADO COM SUCESSO!\n\n");	
	else
	mostrarMensagens( );
	return EXIT_SUCCESS;
}
#line 606 "y.tab.c"

#if YYDEBUG
#include <stdio.h>		/* needed for printf */
#endif

#include <stdlib.h>	/* needed for malloc, etc */
#include <string.h>	/* needed for memset */

/* allocate initial stack or double stack size, up to YYMAXDEPTH */
static int yygrowstack(YYSTACKDATA *data)
{
    int i;
    unsigned newsize;
    short *newss;
    YYSTYPE *newvs;

    if ((newsize = data->stacksize) == 0)
        newsize = YYINITSTACKSIZE;
    else if (newsize >= YYMAXDEPTH)
        return -1;
    else if ((newsize *= 2) > YYMAXDEPTH)
        newsize = YYMAXDEPTH;

    i = data->s_mark - data->s_base;
    newss = (short *)realloc(data->s_base, newsize * sizeof(*newss));
    if (newss == 0)
        return -1;

    data->s_base = newss;
    data->s_mark = newss + i;

    newvs = (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs));
    if (newvs == 0)
        return -1;

    data->l_base = newvs;
    data->l_mark = newvs + i;

    data->stacksize = newsize;
    data->s_last = data->s_base + newsize - 1;
    return 0;
}

#if YYPURE || defined(YY_NO_LEAKS)
static void yyfreestack(YYSTACKDATA *data)
{
    free(data->s_base);
    free(data->l_base);
    memset(data, 0, sizeof(*data));
}
#else
#define yyfreestack(data) /* nothing */
#endif

#define YYABORT  goto yyabort
#define YYREJECT goto yyabort
#define YYACCEPT goto yyaccept
#define YYERROR  goto yyerrlab

int
YYPARSE_DECL()
{
    int yym, yyn, yystate;
#if YYDEBUG
    const char *yys;

    if ((yys = getenv("YYDEBUG")) != 0)
    {
        yyn = *yys;
        if (yyn >= '0' && yyn <= '9')
            yydebug = yyn - '0';
    }
#endif

    yynerrs = 0;
    yyerrflag = 0;
    yychar = YYEMPTY;
    yystate = 0;

#if YYPURE
    memset(&yystack, 0, sizeof(yystack));
#endif

    if (yystack.s_base == NULL && yygrowstack(&yystack)) goto yyoverflow;
    yystack.s_mark = yystack.s_base;
    yystack.l_mark = yystack.l_base;
    yystate = 0;
    *yystack.s_mark = 0;

yyloop:
    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
    if (yychar < 0)
    {
        if ((yychar = YYLEX) < 0) yychar = 0;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, reading %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
    }
    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: state %d, shifting to state %d\n",
                    YYPREFIX, yystate, yytable[yyn]);
#endif
        if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
        {
            goto yyoverflow;
        }
        yystate = yytable[yyn];
        *++yystack.s_mark = yytable[yyn];
        *++yystack.l_mark = yylval;
        yychar = YYEMPTY;
        if (yyerrflag > 0)  --yyerrflag;
        goto yyloop;
    }
    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
        yyn = yytable[yyn];
        goto yyreduce;
    }
    if (yyerrflag) goto yyinrecovery;

    yyerror("syntax error");

    goto yyerrlab;

yyerrlab:
    ++yynerrs;

yyinrecovery:
    if (yyerrflag < 3)
    {
        yyerrflag = 3;
        for (;;)
        {
            if ((yyn = yysindex[*yystack.s_mark]) && (yyn += YYERRCODE) >= 0 &&
                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: state %d, error recovery shifting\
 to state %d\n", YYPREFIX, *yystack.s_mark, yytable[yyn]);
#endif
                if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
                {
                    goto yyoverflow;
                }
                yystate = yytable[yyn];
                *++yystack.s_mark = yytable[yyn];
                *++yystack.l_mark = yylval;
                goto yyloop;
            }
            else
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: error recovery discarding state %d\n",
                            YYPREFIX, *yystack.s_mark);
#endif
                if (yystack.s_mark <= yystack.s_base) goto yyabort;
                --yystack.s_mark;
                --yystack.l_mark;
            }
        }
    }
    else
    {
        if (yychar == 0) goto yyabort;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
        yychar = YYEMPTY;
        goto yyloop;
    }

yyreduce:
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
                YYPREFIX, yystate, yyn, yyrule[yyn]);
#endif
    yym = yylen[yyn];
    if (yym)
        yyval = yystack.l_mark[1-yym];
    else
        memset(&yyval, 0, sizeof yyval);
    switch (yyn)
    {
case 1:
#line 199 "pascal_lr1_syntax_analyser.y"
	{ create_file(strdup(yystack.l_mark[0])); }
break;
case 2:
#line 199 "pascal_lr1_syntax_analyser.y"
	{  create_main(yystack.l_mark[-1]); }
break;
case 5:
#line 204 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 6:
#line 205 "pascal_lr1_syntax_analyser.y"
	{yyval = "";}
break;
case 9:
#line 210 "pascal_lr1_syntax_analyser.y"
	{ yyval = ""; }
break;
case 14:
#line 221 "pascal_lr1_syntax_analyser.y"
	{ 
							for(;rt_tabela>0;rt_tabela--)
							{


									/*
										1- inteiro 2-carac 3-string 4-real 5-bool 6-constante 10-nomedefunção 11-nomedeprocedimento 12-nomedoprograma
									*/

									if (strcmp(yystack.l_mark[-1], "int")==0)
										tabela_de_simbolos[aponta_tab-rt_tabela].tipo = 1;
									else	if (strcmp(yystack.l_mark[-1], "float")==0)
										tabela_de_simbolos[aponta_tab-rt_tabela].tipo = 4;
									else	if (strcmp(yystack.l_mark[-1], "char*")==0)
										tabela_de_simbolos[aponta_tab-rt_tabela].tipo = 2;


							}
							/* reseta contagem de variaveis na lista atual*/
							rt_tabela=0;													
							/* geração de codigo*/
							declaracao_var(strdup(yystack.l_mark[-1]), strdup(yystack.l_mark[-3])); 
							}
break;
case 15:
#line 244 "pascal_lr1_syntax_analyser.y"
	{yyval = ""; }
break;
case 16:
#line 247 "pascal_lr1_syntax_analyser.y"
	{ 
	
										/* verifica se a variavel ja foi declarada naquele escopo									*/
										ct=aponta_tab-1;
										int flag=0; /* indicador se há uma variavel como mesmo nome*/
										for(;ct>=0;ct--)
										{

											if (strcmp(tabela_de_simbolos[ct].nome,yystack.l_mark[0])==0) {flag=1;}
											
										}
										if(flag==1) {printf("\nERRO: Variavel duplicada na linha %d: \"%s\"\nFALHA NA COMPILAÇÃO\n\n",lc,yystack.l_mark[0]); exit(0);}
										else
											{
											/* guardando nome da variavel na tabela de simbolos*/
											tabela_de_simbolos[aponta_tab].escopo = t_escopo;
											tabela_de_simbolos[aponta_tab++].nome = strdup(yystack.l_mark[0]);
											rt_tabela++;
											/*printf("%s \n",$3); // geração de codigo*/
			 								sprintf(yyval, "%s, %s", yystack.l_mark[-2], yystack.l_mark[0]); 
											}
						}
break;
case 17:
#line 269 "pascal_lr1_syntax_analyser.y"
	{ 
								

										/* verifica se a variavel ja foi declarada naquele escopo									*/
										ct=aponta_tab-1;
										int flag=0; /* indicador se há uma variavel como mesmo nome*/
										for(;ct>=0;ct--)
										{

											if (strcmp(tabela_de_simbolos[ct].nome,yystack.l_mark[0])==0) {flag=1;}
											
										}
										if(flag==1) {printf("\nERRO: Variavel duplamente declarada, linha %d: \"%s\"\nFALHA NA COMPILAÇÃO\n\n",lc,yystack.l_mark[0]); exit(0);}
										else
											{

											/* guardando nome da variavel na tabela de simbolos*/
											tabela_de_simbolos[aponta_tab].escopo = t_escopo;
											tabela_de_simbolos[aponta_tab++].nome = strdup(yystack.l_mark[0]);
											rt_tabela++;
											/* geração de codigo*/
											yyval = strdup(yystack.l_mark[0]); 
											}
						}
break;
case 18:
#line 298 "pascal_lr1_syntax_analyser.y"
	{ create_func(yystack.l_mark[-9], yystack.l_mark[-4], yystack.l_mark[-7], yystack.l_mark[-2], yystack.l_mark[-1]); }
break;
case 19:
#line 300 "pascal_lr1_syntax_analyser.y"
	{ create_proc(yystack.l_mark[-7], yystack.l_mark[-5]); }
break;
case 20:
#line 302 "pascal_lr1_syntax_analyser.y"
	{ strcat(yyval, ", "); strcat(yyval, declara_parametros(yystack.l_mark[-2], yystack.l_mark[0]));}
break;
case 21:
#line 303 "pascal_lr1_syntax_analyser.y"
	{ yyval = declara_parametros(yystack.l_mark[-2], yystack.l_mark[0]);  }
break;
case 22:
#line 304 "pascal_lr1_syntax_analyser.y"
	{ yyval = ""; }
break;
case 23:
#line 307 "pascal_lr1_syntax_analyser.y"
	{ yyval = strdup(yystack.l_mark[0]);  }
break;
case 24:
#line 309 "pascal_lr1_syntax_analyser.y"
	{ yyval = "int"; }
break;
case 25:
#line 310 "pascal_lr1_syntax_analyser.y"
	{ yyval = "bool"; }
break;
case 26:
#line 311 "pascal_lr1_syntax_analyser.y"
	{ yyval = "float"; }
break;
case 27:
#line 312 "pascal_lr1_syntax_analyser.y"
	{ yyval = "char*"; }
break;
case 32:
#line 319 "pascal_lr1_syntax_analyser.y"
	{ tipo_herdado = 3 ;yyval = yystack.l_mark[0]; }
break;
case 33:
#line 320 "pascal_lr1_syntax_analyser.y"
	{ tipo_herdado = 2 ;yyval = yystack.l_mark[0]; }
break;
case 34:
#line 321 "pascal_lr1_syntax_analyser.y"
	{ tipo_herdado = 1 ;yyval = yystack.l_mark[0]; }
break;
case 35:
#line 322 "pascal_lr1_syntax_analyser.y"
	{ tipo_herdado = 4 ;yyval = yystack.l_mark[0]; }
break;
case 36:
#line 323 "pascal_lr1_syntax_analyser.y"
	{ tipo_herdado = 5 ;yyval = yystack.l_mark[0]; }
break;
case 46:
#line 349 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[-1]; }
break;
case 47:
#line 352 "pascal_lr1_syntax_analyser.y"
	{ sprintf(yyval, "%s %s", yystack.l_mark[-2], yystack.l_mark[0]); }
break;
case 48:
#line 353 "pascal_lr1_syntax_analyser.y"
	{ yyval = ";";}
break;
case 49:
#line 356 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 50:
#line 357 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 51:
#line 358 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 52:
#line 359 "pascal_lr1_syntax_analyser.y"
	{ yyval = "break"; }
break;
case 53:
#line 360 "pascal_lr1_syntax_analyser.y"
	{ 
														ct=aponta_tab-1;
														 /* procurando variavel na tabela de simbolos*/
														int flag=0;
														for(;ct>=0;ct--)
														{
															
															if (strcmp(tabela_de_simbolos[ct].nome,yystack.l_mark[-2])==0 && t_escopo==tabela_de_simbolos[ct].escopo) {
																flag=1;
																tipo_aux = tabela_de_simbolos[ct].tipo;
																}
											
														}

														if (flag){
															if (tipo_herdado==tipo_aux)
																{
																/* geração de codigo*/
																sprintf(yyval, "%s = %s", yystack.l_mark[-2], yystack.l_mark[0]); 
																}
															else	
																{
																printf("\nERRO: Atribuição de tipos incompatíveis na linha %d: \"%s\" e \"%s\"\nFALHA NA COMPILAÇÃO\n\n",lc,yystack.l_mark[-2],yystack.l_mark[0]); exit(0);
																}
															}
															else
															{printf("\nERRO: Variavel não declarada na linha %d: \"%s\"\nFALHA NA COMPILAÇÃO\n\n",lc,yystack.l_mark[-2]); exit(0);}
													}
break;
case 54:
#line 390 "pascal_lr1_syntax_analyser.y"
	{ 
						ct=aponta_tab-1;
						/* procurando id na tabela de simbolos*/
						int flag=0;						
						for(;ct>=0;ct--)
										{
											if (strcmp(tabela_de_simbolos[ct].nome,yystack.l_mark[0])==0 && t_escopo==tabela_de_simbolos[ct].escopo) 
												{
												tipo_herdado = tabela_de_simbolos[ct].tipo;
												flag = 1;
												break;
												}
										}
					/* a variavel nao foi encontrada na tabela de simbolos	*/
					if (flag==0) {printf("\nERRO: Variavel não declarada na linha %d: \"%s\"\nFALHA NA COMPILAÇÃO\n\n",lc,yystack.l_mark[0]); exit(0);}
											



				}
break;
case 59:
#line 416 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 60:
#line 419 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 61:
#line 420 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 62:
#line 421 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 63:
#line 422 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 64:
#line 423 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 65:
#line 424 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 66:
#line 425 "pascal_lr1_syntax_analyser.y"
	{ yyval = "=="; }
break;
case 67:
#line 426 "pascal_lr1_syntax_analyser.y"
	{ yyval = "!="; }
break;
case 68:
#line 427 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 69:
#line 428 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 70:
#line 429 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 71:
#line 430 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 73:
#line 434 "pascal_lr1_syntax_analyser.y"
	{ sprintf(yyval, "!%s", yystack.l_mark[0]); }
break;
case 74:
#line 435 "pascal_lr1_syntax_analyser.y"
	{ sprintf(yyval, "+ %s", yystack.l_mark[0]); }
break;
case 75:
#line 436 "pascal_lr1_syntax_analyser.y"
	{ sprintf(yyval, "- %s", yystack.l_mark[0]); }
break;
case 76:
#line 437 "pascal_lr1_syntax_analyser.y"
	{ sprintf(yyval, "( %s )", yystack.l_mark[-1]); }
break;
case 77:
#line 438 "pascal_lr1_syntax_analyser.y"
	{ yyval = yystack.l_mark[0]; }
break;
case 78:
#line 441 "pascal_lr1_syntax_analyser.y"
	{ create_if(yystack.l_mark[-3], yystack.l_mark[-1]); }
break;
#line 1132 "y.tab.c"
    }
    yystack.s_mark -= yym;
    yystate = *yystack.s_mark;
    yystack.l_mark -= yym;
    yym = yylhs[yyn];
    if (yystate == 0 && yym == 0)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: after reduction, shifting from state 0 to\
 state %d\n", YYPREFIX, YYFINAL);
#endif
        yystate = YYFINAL;
        *++yystack.s_mark = YYFINAL;
        *++yystack.l_mark = yyval;
        if (yychar < 0)
        {
            if ((yychar = YYLEX) < 0) yychar = 0;
#if YYDEBUG
            if (yydebug)
            {
                yys = 0;
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
                if (!yys) yys = "illegal-symbol";
                printf("%sdebug: state %d, reading %d (%s)\n",
                        YYPREFIX, YYFINAL, yychar, yys);
            }
#endif
        }
        if (yychar == 0) goto yyaccept;
        goto yyloop;
    }
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
        yystate = yytable[yyn];
    else
        yystate = yydgoto[yym];
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: after reduction, shifting from state %d \
to state %d\n", YYPREFIX, *yystack.s_mark, yystate);
#endif
    if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
    {
        goto yyoverflow;
    }
    *++yystack.s_mark = (short) yystate;
    *++yystack.l_mark = yyval;
    goto yyloop;

yyoverflow:
    yyerror("yacc stack overflow");

yyabort:
    yyfreestack(&yystack);
    return (1);

yyaccept:
    yyfreestack(&yystack);
    return (0);
}
